//
//  LQBigPicturePresentController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/20.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

protocol LQPhotoBrowserAnimatorDelegate:NSObjectProtocol {    /// 参与转场动画的图像视图
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 参与转场动画的全新图像视图
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    
    /// 展现转场图像起始位置
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 相对于屏幕的位置
    func photoBrowserPresentFromRect(indexPath: NSIndexPath) -> CGRect
    
    /// 展现动画结束时的屏幕位置
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 相对于屏幕的位置
    func photoBrowserPresentToRect(indexPath: NSIndexPath) -> CGRect
    
}

// MARK: - 照片浏览器解除转场代理
protocol LQPhotoBrowserDismissDelegate: NSObjectProtocol {
    
    /// 解除转场动画的图像视图
    func imageViewForDismiss() -> UIImageView
    /// 解除转场动画的图像索引
    func indexPathForDismiss() -> NSIndexPath
}

class LQPhotoBrowserAnimator:NSObject,UIViewControllerTransitioningDelegate{
    
    /// 展现动画代理
    weak var presentDelegate: LQPhotoBrowserAnimatorDelegate?
    weak var dismissDelegate:LQPhotoBrowserDismissDelegate?
    /// 显示的图片的索引
    var indexPath:NSIndexPath?
    
    var isPresent = true
     func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
   func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
    /// 设置展现代理
    ///
    /// - parameter presentDelegate: 展现代理
    /// - parameter indexPath:       显示的图片的索引
    func setPresentDelegate (presentDelegate:LQPhotoBrowserAnimatorDelegate,dismissDelegate:LQPhotoBrowserDismissDelegate,indexPath: NSIndexPath) {
        self.dismissDelegate = dismissDelegate
        self.presentDelegate = presentDelegate
        self.indexPath = indexPath
    }
}

extension LQPhotoBrowserAnimator:UIViewControllerAnimatedTransitioning  {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
//        print(containerView)
//        containerView?.addSubview(UIButton(type: UIButtonType.ContactAdd))
        
//        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
//        print(fromVC)
//        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
//        print(toVC)
//        let fromeView = transitionContext.viewForKey(UITransitionContextFromViewKey)
//        print(fromeView)
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        print("------\(toView)")
        if isPresent {
            presentAnimator(transitionContext)
        } else {
            dismissAnimator(transitionContext)
        }
        
    }
}
// MARK: - PresentAnimator
extension LQPhotoBrowserAnimator {
    private func presentAnimator (transitionContext: UIViewControllerContextTransitioning) {
        guard let presentDelegate = presentDelegate,indexPath = indexPath else {
            return
        }

        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
//        presentView.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
        transitionContext.containerView()?.addSubview(presentView)
        presentView.alpha = 0
//        
//        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
//        // 2> 添加到容器视图
//        transitionContext.containerView()?.addSubview(toView)
//        // 3> 设置透明度
//        toView.alpha = 0
        
        let animatorView = presentDelegate.imageViewForPresent(indexPath)
        
        animatorView.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
        transitionContext.containerView()?.addSubview(animatorView)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            animatorView.frame = presentDelegate.photoBrowserPresentToRect(indexPath)
            animatorView.alpha = 0
            presentView.alpha = 1
            
            }) { (_) -> Void in
                animatorView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
        

       
    }
}
// MARK: - DidmissAnimator
extension LQPhotoBrowserAnimator {
    private func dismissAnimator (transitionContext: UIViewControllerContextTransitioning) {
        print("miss")
        guard let presentDelegate = presentDelegate, dismissDelegate = dismissDelegate else {
            return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        // 2. 转场动画中的大`米视图
        let iv = dismissDelegate.imageViewForDismiss()
        let indexPath = dismissDelegate.indexPathForDismiss()
        transitionContext.containerView()?.addSubview(iv)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            
            fromView.alpha = 0
            iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
            
            }) { _ in
                
                iv.removeFromSuperview()
                fromView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }

    }
